import { Injectable } from '@nestjs/common'
import { HttpService } from '@nestjs/axios'
import { type Observable, lastValueFrom } from 'rxjs'
import {
  MeetingRoomListReq,
  MeetingRoomListRes,
  PredetermineListRes,
  CancelMeetingRoomReq,
  DueMeetingRoomReq,
  GetMeetingRoomDetailReq,
  GetMeetingRoomDetailRes
} from '../interfaces'
import { AxiosResponse, InternalAxiosRequestConfig } from 'axios'
import { convertKeysToCamelCaseDeep } from '@/utils'

const cookie =
  'oa_session=jem1dgl0ig4s9huabv5unog596; mid=72187; channel_extend_info={}; session=3410718f4dcf4c39a25da9aa98862871; fp=14c0a9c89b926dbe771b94ce6d9013c9; oa_token_id=8XTfTKV5y%2FNV60Wfydim7Gq8Rj9xR3iIx9bBiqMES7ONzAHcXOVuuNUJ%2FT3%2BA5dmjhwo8zzKEw0%2Bw3FoKIgPXQ%3D%3D'
const MOA_BASE_URL = 'https://m.oa.fenqile.com'

@Injectable()
export class ApiService {
  constructor(private readonly httpService: HttpService) {
    // 添加请求拦截器
    this.httpService.axiosRef.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        // 可以在这里添加 token 或其他请求头
        config.headers['Cookie'] = cookie
        return config
      },
      (error) => {
        // 请求错误处理
        return Promise.reject(error)
      }
    )

    // 添加响应拦截器
    this.httpService.axiosRef.interceptors.response.use(
      (response: AxiosResponse) => {
        // 可以对响应数据进行处理
        return convertKeysToCamelCaseDeep(response.data)
      },
      (error) => {
        // 响应错误处理
        return Promise.reject(error)
      }
    )
  }

  // 查询某个分公司的所有会议室，以及会议室时间段和占用情况
  async getMeetingRoomList(
    data: MeetingRoomListReq
  ): Promise<Observable<AxiosResponse<MeetingRoomListRes>>> {
    return this.httpService.get(
      `${MOA_BASE_URL}/restful/get/meeting/meeting_room_address_room.json`,
      {
        params: data
      }
    )
  }

  // 查询城市和分公司列表
  async getPredetermineList(): Promise<
    Observable<AxiosResponse<PredetermineListRes>>
  > {
    return this.httpService.get(
      `${MOA_BASE_URL}/meeting/main/predetermine.json`,
      {
        headers: {
          Cookie: cookie
        }
      }
    )
  }

  // 取消会议室
  async cancelMeetingRoom(
    data: CancelMeetingRoomReq
  ): Promise<Observable<AxiosResponse<null>>> {
    return this.httpService.get(
      `${MOA_BASE_URL}/restful/get/meeting/meeting_room_cancel_meeting.json`,
      {
        params: data
      }
    )
  }

  // 预订会议室（由服务端向MOA发出的请求）
  async dueMeetingRoom(data: DueMeetingRoomReq): Promise<AxiosResponse<null>> {
    return await lastValueFrom(
      this.httpService.get(`${MOA_BASE_URL}/meeting/main/due_meeting.json`, {
        params: data
      })
    )
  }

  // 查询某个会议室详情
  async getMeetingRoomDetail(
    data: GetMeetingRoomDetailReq
  ): Promise<Observable<AxiosResponse<GetMeetingRoomDetailRes>>> {
    return this.httpService.get(
      `${MOA_BASE_URL}/meeting/main/room_detail.json`,
      {
        params: data
      }
    )
  }

  // 查询所有分公司（用于快速预订模式）
  async getMeetingRoomAddress(): Promise<
    Observable<AxiosResponse<GetMeetingRoomDetailRes>>
  > {
    return this.httpService.get(
      `${MOA_BASE_URL}/restful/get/meeting/meeting_room_query_address.json`
    )
  }
}
